package org.dbstar.scheduler.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dbstar.scheduler.identifier.Identifier;
import org.dbstar.scheduler.identifier.IdentifierType;


/**
 * Job依赖与触发
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-6-13
 */
public class JobDependency implements Serializable, Cloneable, Comparable<JobDependency> {
	private static final long serialVersionUID = -55750131861998479L;

	private Identifier identifier;// 依赖标识
	private Identifier jobConfIdentifier;// 触发Job的定义标识
	private String name;// Job依赖触发的名称
	private String description;// Job依赖触发的描述
	private List<String> keys;// 触发Job的关键参数
	private Map<String, String> parameters;// 触发Job的其他参数
	private Set<JobDependencyItem> dependencies;// 依赖项
	private long createTime = System.currentTimeMillis();// 创建时间
	private Long modifyTime;// 最后修改时间
	private String owner;// Job依赖触发的所有者，默认为创建人
	private String modifier;// 最后修改人

	public Identifier getIdentifier() {
		return identifier;
	}

	public void setIdentifier(Identifier identifier) {
		if (identifier == null) {
			throw new IllegalArgumentException("Identifier must not null.");
		}
		if (identifier.getIdentifierType() != IdentifierType.depend) {
			throw new IllegalArgumentException("Identifier not a Dependency identifier: "
					+ identifier.getStringIdentifier());
		}
		this.identifier = identifier;
	}

	public Identifier getJobConfIdentifier() {
		return jobConfIdentifier;
	}

	public void setJobConfIdentifier(Identifier jobConfIdentifier) {
		if (jobConfIdentifier == null) {
			throw new IllegalArgumentException("Identifier must not null.");
		}
		if (jobConfIdentifier.getIdentifierType() != IdentifierType.conf) {
			throw new IllegalArgumentException("Identifier not a Job Configuration identifier: "
					+ jobConfIdentifier.getStringIdentifier());
		}
		this.jobConfIdentifier = jobConfIdentifier;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getKeySize() {
		return keys == null ? 0 : keys.size();
	}

	public List<String> getKeys() {
		return keys;
	}

	public void setKeys(List<String> keys) {
		this.keys = keys == null || keys.isEmpty() ? null : keys;
	}

	public int getParameterSize() {
		return parameters == null ? 0 : parameters.size();
	}

	public Map<String, String> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, String> parameters) {
		this.parameters = parameters == null || parameters.isEmpty() ? null : parameters;
	}

	public int getDependencySize() {
		return dependencies == null ? 0 : dependencies.size();
	}

	public Set<JobDependencyItem> getDependencies() {
		return dependencies;
	}

	public void setDependencies(Set<JobDependencyItem> dependencies) {
		this.dependencies = dependencies == null || dependencies.isEmpty() ? null : dependencies;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public Long getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Long modifyTime) {
		this.modifyTime = modifyTime;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		JobDependency other = (JobDependency) obj;
		if (identifier == null) {
			if (other.identifier != null)
				return false;
		} else if (!identifier.equals(other.identifier))
			return false;
		return true;
	}

	@Override
	public int compareTo(JobDependency o) {
		int result = jobConfIdentifier.compareTo(o.jobConfIdentifier);
		return result == 0 ? identifier.compareTo(o.identifier) : result;
	}

	@Override
	public JobDependency clone() throws CloneNotSupportedException {
		JobDependency dependency = (JobDependency) super.clone();
		if (keys != null) {
			dependency.keys = new ArrayList<String>(keys);
		}
		if (parameters != null) {
			dependency.parameters = new HashMap<String, String>(parameters);
		}
		if (dependencies != null) {
			dependency.dependencies = new HashSet<JobDependencyItem>(dependencies);
		}
		return dependency;
	}
}
